package langchain4j.hsl.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import langchain4j.hsl.com.mapper.DoctorScheduleMapper;
import langchain4j.hsl.com.model.entity.Appointment;
import langchain4j.hsl.com.model.entity.DoctorSchedule;
import langchain4j.hsl.com.service.DoctorScheduleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 医生排班服务实现类
 */
@Service
public class DoctorScheduleServiceImpl extends ServiceImpl<DoctorScheduleMapper, DoctorSchedule>
        implements DoctorScheduleService {

    // 检查指定条件下是否有号源可用
    @Override
    public boolean checkAvailability(Appointment appointment) {
        LambdaQueryWrapper<DoctorSchedule> queryWrapper = new LambdaQueryWrapper<>();

        // 构建查询条件：科室、日期、时间段必须匹配
        queryWrapper.eq(DoctorSchedule::getDepartment, appointment.getDepartment())
                .eq(DoctorSchedule::getScheduleDate, appointment.getDate())
                .eq(DoctorSchedule::getTimeSlot, appointment.getTime())
                .eq(DoctorSchedule::getStatus, 1); // 只查询有效的排班

        // 如果指定了医生，则添加医生条件
        if (StringUtils.hasText(appointment.getDoctorName())) {
            queryWrapper.eq(DoctorSchedule::getDoctorName, appointment.getDoctorName());

            // 查询特定医生的排班
            DoctorSchedule schedule = this.getOne(queryWrapper);
            // 检查该医生在指定时间是否有排班且有剩余容量
            return schedule != null && schedule.getRemainingCapacity() > 0;
        } else {
            // 未指定医生，查询该科室在指定时间是否有任何医生可预约
            queryWrapper.gt(DoctorSchedule::getRemainingCapacity, 0);
            return this.count(queryWrapper) > 0;
        }
    }

    // 获取指定条件下所有可预约的医生列表
    @Override
    public List<DoctorSchedule> getAvailableDoctors(Appointment appointment) {
        LambdaQueryWrapper<DoctorSchedule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DoctorSchedule::getDepartment, appointment.getDepartment())
                .eq(DoctorSchedule::getScheduleDate, appointment.getDate())
                .eq(DoctorSchedule::getTimeSlot, appointment.getTime())
                .eq(DoctorSchedule::getStatus, 1)
                .gt(DoctorSchedule::getRemainingCapacity, 0);

        return this.list(queryWrapper);
    }

    // 获取特定医生的排班信息
    @Override
    public DoctorSchedule getSchedule(Appointment appointment) {
        LambdaQueryWrapper<DoctorSchedule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DoctorSchedule::getDepartment, appointment.getDepartment())
                .eq(DoctorSchedule::getScheduleDate, appointment.getDate())
                .eq(DoctorSchedule::getTimeSlot, appointment.getTime())
                .eq(DoctorSchedule::getDoctorName, appointment.getDoctorName())
                .eq(DoctorSchedule::getStatus, 1);

        return this.getOne(queryWrapper);
    }

    // 成功预约后减少剩余容量
    @Override
    public boolean decreaseCapacity(Long scheduleId) {
        DoctorSchedule schedule = this.getById(scheduleId);
        if (schedule == null || schedule.getRemainingCapacity() <= 0) {
            return false;
        }

        // 减少剩余容量
        schedule.setRemainingCapacity(schedule.getRemainingCapacity() - 1);
        return this.updateById(schedule);
    }

    // 取消预约后增加剩余容量
    @Override
    public boolean increaseCapacity(Long scheduleId) {
        DoctorSchedule schedule = this.getById(scheduleId);
        if (schedule == null) {
            return false;
        }

        // 确保不超过总容量
        if (schedule.getRemainingCapacity() < schedule.getTotalCapacity()) {
            schedule.setRemainingCapacity(schedule.getRemainingCapacity() + 1);
            return this.updateById(schedule);
        }

        return true; // 已经是最大容量，无需增加
    }
}